package com.sxhalo.fsms.controller;


import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;

import com.sxhalo.fsms.service.IBaseService;
import com.sxhalo.fsms.util.ApiResult;
import com.sxhalo.fsms.util.PageData;


public class BaseController {
	
	@Autowired
	private HttpServletRequest req;



	public ApiResult findList(Integer current, Integer pageSize, String where, String order, IBaseService<?> service) {
		if (current == null && pageSize == null) {
			List<?> list = service.findWhereList(where, order);
			return ApiResult.retOK(req, list);
		} else {
			Integer start = pageSize * (current - 1);
			Integer count = service.getCount(where);
			List<?> list = service.findPagerList(start, pageSize, where, order);
			PageData page = new PageData(current, pageSize, count, list);
			return ApiResult.retOK(req, page);
		}
	}
	
	public boolean isBlank(String s) {
		if (s == null) {
			return true;
		}
		if (s.equals("null")) {
			return true;
		}
		s = s.trim();
		if (s.length() == 0) {
			return true;
		}
		return false;
	}


	public String filterString(String str) throws PatternSyntaxException {
		String s = "";
		if (str != null && !str.isEmpty()) {
			String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？\\\\]";
			Pattern p = Pattern.compile(regEx);
			Matcher m = p.matcher(str);
			s = m.replaceAll("_").trim();
		}
		return s;
	}


}
